﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace SystemDiagramNew
{
	public partial class SetPipeElevation : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public SetPipeElevation()
		{
			this.InitializeComponent();
		}

		private void SetPipeElevation_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.precisionValue.SelectedIndex = PipeElevationSettingData.Instance.Precision;
			this.prefixValue.Text = PipeElevationSettingData.Instance.Prefix;
			this.customElevationText.Checked = PipeElevationSettingData.Instance.IsCustom;
			if (this.customElevationText.Checked)
			{
				this.customElevationValue.Enabled = true;
			}
			else
			{
				this.customElevationValue.Enabled = false;
			}
			this.deleteOldGroups.Checked = PipeElevationSettingData.Instance.IsDelteOldElevations;
			this.customElevationValue.Text = PipeElevationSettingData.Instance.CustomText;
			if (PipeElevationSettingData.Instance.MarkLocation == ElevationLabelLocation.OuterPipe)
			{
				this.outerPipe.Checked = true;
				return;
			}
			this.innerPipe.Checked = true;
		}

		public string GetElevationText()
		{
			return string.Empty;
		}

		private void customElevationText_CheckedChanged(object sender, EventArgs e)
		{
			if (this.customElevationText.Checked)
			{
				this.customElevationValue.Enabled = true;
				return;
			}
			this.customElevationValue.Enabled = false;
		}

		private void customElevationValue_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.customElevationValue.Text))
			{
				return;
			}
			try
			{
				if (Convert.ToDouble(this.customElevationValue.Text) < 0.0)
				{
					YJKMessageBox.Error("自定义标高必须大于0，请重新输入!");
					this.customElevationValue.Clear();
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("自定义标高必须是数值，请重新输入!");
				this.customElevationValue.Clear();
			}
		}

		private void outerPipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.outerPipe.Checked)
			{
				this.labelLocation = ElevationLabelLocation.OuterPipe;
				this.isPicking = true;
				this.SwithStatus();
				this.isPicking = true;
			}
		}

		private void innerPipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.innerPipe.Checked)
			{
				this.labelLocation = ElevationLabelLocation.InnerPipe;
				this.isPicking = true;
				this.SwithStatus();
				this.isPicking = true;
			}
		}

		private void customElevationValue_Leave(object sender, EventArgs e)
		{
		}

		private void customElevationValue_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void customElevationValue_Validated(object sender, EventArgs e)
		{
		}

		protected void SwithStatus()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 10);
			this.SetFocusToMainWnd();
			this.PressESCKey();
		}

		protected void SetFocusToMainWnd()
		{
			int wMsg = 7;
			SetPipeElevation.SendMessage(CmdSetPipeElevation.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		protected void PressESCKey()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SetPipeElevation.keybd_event((byte)bVk, 0, dwFlags, 0);
			SetPipeElevation.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SetPipeElevation_FormClosed(object sender, FormClosedEventArgs e)
		{
			PipeElevationSettingData.Instance.Precision = this.precisionValue.SelectedIndex;
			PipeElevationSettingData.Instance.Prefix = this.prefixValue.Text;
			PipeElevationSettingData.Instance.IsCustom = this.customElevationText.Checked;
			PipeElevationSettingData.Instance.CustomText = this.customElevationValue.Text;
			PipeElevationSettingData.Instance.MarkLocation = (this.outerPipe.Checked ? ElevationLabelLocation.OuterPipe : ElevationLabelLocation.InnerPipe);
			PipeElevationSettingData.Instance.IsDelteOldElevations = this.deleteOldGroups.Checked;
			this.PressESCKey();
			this.isPicking = false;
		}

		private void customElevationValue_Validating(object sender, CancelEventArgs e)
		{
		}

		public ElevationLabelLocation labelLocation;

		public static readonly int offset = 2;

		public bool isPicking = true;
	}
}
